﻿Public Class Form1


    Private _SuspendUpdate As Boolean = False

    Private WithEvents _tmpHDRBmp As TheiaLib.Imaging.HDRfiles.RadianceHDR_Image

    Private Sub UpdateColorSlider()
        Dim tmpBMP As TheiaLib.Imaging.GDIBitmap
        Dim pos As Integer

        Dim RGB As New TheiaLib.Imaging.RGBTriplet(nudRed.Value, nudGreen.Value, nudBlue.Value)
        Dim HSV As New TheiaLib.Imaging.HSVTriplet(nudHue.Value, nudSaturation.Value, nudValue.Value)
        Dim Lab As New TheiaLib.Imaging.LabTriplet(nudLuminance.Value, nudA.Value, nudB.Value)
        Dim XYZ As New TheiaLib.Imaging.XYZTriplet(nudX.Value, nudY.Value, nudZ.Value)


        ' Selected Color
        tmpBMP = New TheiaLib.Imaging.GDIBitmap(PictureBox2.Image)
        Dim p As Drawing.Color = Color.FromArgb(Math.Round(RGB.Red * 255), Math.Round(RGB.Green * 255), Math.Round(RGB.Blue * 255))

        For x As Integer = 0 To tmpBMP.Width - 1
            For y As Integer = 0 To tmpBMP.Height - 1
                tmpBMP.Pixel(x, y) = p
            Next
        Next
        tmpBMP.UnLockBitmap()


        ' RED
        tmpBMP = New TheiaLib.Imaging.GDIBitmap(pbRed.Image)
        For x As Integer = 0 To tmpBMP.Width - 1
            p = Color.FromArgb(Math.Round(x / tmpBMP.Width * 255), Math.Round(RGB.Green * 255), Math.Round(RGB.Blue * 255))
            For y As Integer = 0 To tmpBMP.Height - 1
                tmpBMP.Pixel(x, y) = p
            Next
        Next
        pos = RGB.Red * (tmpBMP.Width - 1)
        For x As Integer = (tmpBMP.Height / 3) * -1 To (tmpBMP.Height / 3)
            For y As Integer = tmpBMP.Height / 3 * 2 + Math.Abs(x) To tmpBMP.Height - 1
                tmpBMP.Pixel2(pos + x, y) = Color.Silver
            Next
        Next
        For y As Integer = 0 To tmpBMP.Height - 1 : tmpBMP.Pixel2(pos, y) = Color.Silver : Next
        tmpBMP.UnLockBitmap()

        ' GREEN
        tmpBMP = New TheiaLib.Imaging.GDIBitmap(pbGreen.Image)
        For x As Integer = 0 To tmpBMP.Width - 1
            p = Color.FromArgb(Math.Round(RGB.Red * 255), Math.Round(x / tmpBMP.Width * 255), Math.Round(RGB.Blue * 255))
            For y As Integer = 0 To tmpBMP.Height - 1
                tmpBMP.Pixel(x, y) = p
            Next
        Next
        pos = RGB.Green * (tmpBMP.Width - 1)
        For x As Integer = (tmpBMP.Height / 3) * -1 To (tmpBMP.Height / 3)
            For y As Integer = tmpBMP.Height / 3 * 2 + Math.Abs(x) To tmpBMP.Height - 1
                tmpBMP.Pixel2(pos + x, y) = Color.Silver
            Next
        Next
        For y As Integer = 0 To tmpBMP.Height - 1 : tmpBMP.Pixel2(pos, y) = Color.Silver : Next
        tmpBMP.UnLockBitmap()

        ' BLUE
        tmpBMP = New TheiaLib.Imaging.GDIBitmap(pbBlue.Image)
        For x As Integer = 0 To tmpBMP.Width - 1
            p = Color.FromArgb(Math.Round(RGB.Red * 255), Math.Round(RGB.Green * 255), Math.Round(x / tmpBMP.Width * 255))
            For y As Integer = 0 To tmpBMP.Height - 1
                tmpBMP.Pixel(x, y) = p
            Next
        Next
        pos = RGB.Blue * (tmpBMP.Width - 1)
        For x As Integer = (tmpBMP.Height / 3) * -1 To (tmpBMP.Height / 3)
            For y As Integer = tmpBMP.Height / 3 * 2 + Math.Abs(x) To tmpBMP.Height - 1
                tmpBMP.Pixel2(pos + x, y) = Color.Silver
            Next
        Next
        For y As Integer = 0 To tmpBMP.Height - 1 : tmpBMP.Pixel2(pos, y) = Color.Silver : Next
        tmpBMP.UnLockBitmap()

        ' HUE
        tmpBMP = New TheiaLib.Imaging.GDIBitmap(pbHue.Image)
        For x As Integer = 0 To tmpBMP.Width - 1
            RGB = TheiaLib.Imaging.ColorSpaceConverter.HSV2RGB(New TheiaLib.Imaging.HSVTriplet(x / tmpBMP.Width, HSV.Saturation, HSV.Value))
            p = Color.FromArgb(Math.Round(RGB.Red * 255), Math.Round(RGB.Green * 255), Math.Round(RGB.Blue * 255))
            For y As Integer = 0 To tmpBMP.Height - 1
                tmpBMP.Pixel(x, y) = p
            Next
        Next
        pos = HSV.Hue * (tmpBMP.Width - 1)
        For x As Integer = (tmpBMP.Height / 3) * -1 To (tmpBMP.Height / 3)
            For y As Integer = tmpBMP.Height / 3 * 2 + Math.Abs(x) To tmpBMP.Height - 1
                tmpBMP.Pixel2(pos + x, y) = Color.Silver
            Next
        Next
        For y As Integer = 0 To tmpBMP.Height - 1 : tmpBMP.Pixel2(pos, y) = Color.Silver : Next
        tmpBMP.UnLockBitmap()

        ' SATURATION
        tmpBMP = New TheiaLib.Imaging.GDIBitmap(pbSaturation.Image)
        For x As Integer = 0 To tmpBMP.Width - 1
            RGB = TheiaLib.Imaging.ColorSpaceConverter.HSV2RGB(New TheiaLib.Imaging.HSVTriplet(HSV.Hue, x / tmpBMP.Width, HSV.Value))
            p = Color.FromArgb(Math.Round(RGB.Red * 255), Math.Round(RGB.Green * 255), Math.Round(RGB.Blue * 255))
            For y As Integer = 0 To tmpBMP.Height - 1
                tmpBMP.Pixel(x, y) = p
            Next
        Next
        pos = HSV.Saturation * (tmpBMP.Width - 1)
        For x As Integer = (tmpBMP.Height / 3) * -1 To (tmpBMP.Height / 3)
            For y As Integer = tmpBMP.Height / 3 * 2 + Math.Abs(x) To tmpBMP.Height - 1
                tmpBMP.Pixel2(pos + x, y) = Color.Silver
            Next
        Next
        For y As Integer = 0 To tmpBMP.Height - 1 : tmpBMP.Pixel2(pos, y) = Color.Silver : Next
        tmpBMP.UnLockBitmap()

        ' VALUE
        tmpBMP = New TheiaLib.Imaging.GDIBitmap(pbValue.Image)
        For x As Integer = 0 To tmpBMP.Width - 1
            RGB = TheiaLib.Imaging.ColorSpaceConverter.HSV2RGB(New TheiaLib.Imaging.HSVTriplet(HSV.Hue, HSV.Saturation, x / tmpBMP.Width))
            p = Color.FromArgb(Math.Round(RGB.Red * 255), Math.Round(RGB.Green * 255), Math.Round(RGB.Blue * 255))
            For y As Integer = 0 To tmpBMP.Height - 1
                tmpBMP.Pixel(x, y) = p
            Next
        Next
        pos = HSV.Value * (tmpBMP.Width - 1)
        For x As Integer = (tmpBMP.Height / 3) * -1 To (tmpBMP.Height / 3)
            For y As Integer = tmpBMP.Height / 3 * 2 + Math.Abs(x) To tmpBMP.Height - 1
                tmpBMP.Pixel2(pos + x, y) = Color.Silver
            Next
        Next
        For y As Integer = 0 To tmpBMP.Height - 1 : tmpBMP.Pixel2(pos, y) = Color.Silver : Next
        tmpBMP.UnLockBitmap()


        ' LUMINANCE
        tmpBMP = New TheiaLib.Imaging.GDIBitmap(pbLuminance.Image)
        For x As Integer = 0 To tmpBMP.Width - 1
            RGB = TheiaLib.Imaging.ColorSpaceConverter.Lab2RGB(New TheiaLib.Imaging.LabTriplet(x / tmpBMP.Width, Lab.a, Lab.b))
            p = Color.FromArgb(Math.Min(Math.Max(Math.Round(RGB.Red * 255), 0), 255), _
                                                    Math.Min(Math.Max(Math.Round(RGB.Green * 255), 0), 255), _
                                                    Math.Min(Math.Max(Math.Round(RGB.Blue * 255), 0), 255))
            For y As Integer = 0 To tmpBMP.Height - 1
                tmpBMP.Pixel(x, y) = p
            Next
        Next
        pos = Lab.Luminance * (tmpBMP.Width - 1)
        For x As Integer = (tmpBMP.Height / 3) * -1 To (tmpBMP.Height / 3)
            For y As Integer = tmpBMP.Height / 3 * 2 + Math.Abs(x) To tmpBMP.Height - 1
                tmpBMP.Pixel2(pos + x, y) = Color.Silver
            Next
        Next
        For y As Integer = 0 To tmpBMP.Height - 1 : tmpBMP.Pixel2(pos, y) = Color.Silver : Next
        tmpBMP.UnLockBitmap()

        ' A
        tmpBMP = New TheiaLib.Imaging.GDIBitmap(pbA.Image)
        For x As Integer = 0 To tmpBMP.Width - 1
            RGB = TheiaLib.Imaging.ColorSpaceConverter.Lab2RGB(New TheiaLib.Imaging.LabTriplet(Lab.Luminance, (x / tmpBMP.Width * 2.4) - 1.2, Lab.b))
            p = Color.FromArgb(Math.Min(Math.Max(Math.Round(RGB.Red * 255), 0), 255), _
                                                    Math.Min(Math.Max(Math.Round(RGB.Green * 255), 0), 255), _
                                                    Math.Min(Math.Max(Math.Round(RGB.Blue * 255), 0), 255))
            For y As Integer = 0 To tmpBMP.Height - 1
                tmpBMP.Pixel(x, y) = p
            Next
        Next
        pos = (Lab.a + 1.2) / 2.4 * (tmpBMP.Width - 1)
        For x As Integer = (tmpBMP.Height / 3) * -1 To (tmpBMP.Height / 3)
            For y As Integer = tmpBMP.Height / 3 * 2 + Math.Abs(x) To tmpBMP.Height - 1
                tmpBMP.Pixel2(pos + x, y) = Color.Silver
            Next
        Next
        For y As Integer = 0 To tmpBMP.Height - 1 : tmpBMP.Pixel2(pos, y) = Color.Silver : Next
        tmpBMP.UnLockBitmap()

        ' B
        tmpBMP = New TheiaLib.Imaging.GDIBitmap(pbB.Image)
        For x As Integer = 0 To tmpBMP.Width - 1
            RGB = TheiaLib.Imaging.ColorSpaceConverter.Lab2RGB(New TheiaLib.Imaging.LabTriplet(Lab.Luminance, Lab.a, (x / tmpBMP.Width * 2.4) - 1.2))
            p = Color.FromArgb(Math.Min(Math.Max(Math.Round(RGB.Red * 255), 0), 255), _
                                                    Math.Min(Math.Max(Math.Round(RGB.Green * 255), 0), 255), _
                                                    Math.Min(Math.Max(Math.Round(RGB.Blue * 255), 0), 255))
            For y As Integer = 0 To tmpBMP.Height - 1
                tmpBMP.Pixel(x, y) = p
            Next
        Next
        pos = (Lab.b + 1.2) / 2.4 * (tmpBMP.Width - 1)
        For x As Integer = (tmpBMP.Height / 3) * -1 To (tmpBMP.Height / 3)
            For y As Integer = tmpBMP.Height / 3 * 2 + Math.Abs(x) To tmpBMP.Height - 1
                tmpBMP.Pixel2(pos + x, y) = Color.Silver
            Next
        Next
        For y As Integer = 0 To tmpBMP.Height - 1 : tmpBMP.Pixel2(pos, y) = Color.Silver : Next
        tmpBMP.UnLockBitmap()

        ' X
        tmpBMP = New TheiaLib.Imaging.GDIBitmap(pbX.Image)
        For x As Integer = 0 To tmpBMP.Width - 1
            RGB = TheiaLib.Imaging.ColorSpaceConverter.XYZ2RGB(New TheiaLib.Imaging.XYZTriplet(x / tmpBMP.Width, XYZ.Y, XYZ.Z))
            p = Color.FromArgb(Math.Min(Math.Max(Math.Round(RGB.Red * 255), 0), 255), _
                               Math.Min(Math.Max(Math.Round(RGB.Green * 255), 0), 255), _
                               Math.Min(Math.Max(Math.Round(RGB.Blue * 255), 0), 255))
            For y As Integer = 0 To tmpBMP.Height - 1
                tmpBMP.Pixel(x, y) = p
            Next
        Next
        pos = XYZ.X * (tmpBMP.Width - 1)
        For x As Integer = (tmpBMP.Height / 3) * -1 To (tmpBMP.Height / 3)
            For y As Integer = tmpBMP.Height / 3 * 2 + Math.Abs(x) To tmpBMP.Height - 1
                tmpBMP.Pixel2(pos + x, y) = Color.Silver
            Next
        Next
        For y As Integer = 0 To tmpBMP.Height - 1 : tmpBMP.Pixel2(pos, y) = Color.Silver : Next
        tmpBMP.UnLockBitmap()

        ' y
        tmpBMP = New TheiaLib.Imaging.GDIBitmap(pbY.Image)
        For x As Integer = 0 To tmpBMP.Width - 1
            RGB = TheiaLib.Imaging.ColorSpaceConverter.XYZ2RGB(New TheiaLib.Imaging.XYZTriplet(XYZ.X, x / tmpBMP.Width, XYZ.Z))
            p = Color.FromArgb(Math.Min(Math.Max(Math.Round(RGB.Red * 255), 0), 255), _
                               Math.Min(Math.Max(Math.Round(RGB.Green * 255), 0), 255), _
                               Math.Min(Math.Max(Math.Round(RGB.Blue * 255), 0), 255))
            For y As Integer = 0 To tmpBMP.Height - 1
                tmpBMP.Pixel(x, y) = p
            Next
        Next
        pos = XYZ.Y * (tmpBMP.Width - 1)
        For x As Integer = (tmpBMP.Height / 3) * -1 To (tmpBMP.Height / 3)
            For y As Integer = tmpBMP.Height / 3 * 2 + Math.Abs(x) To tmpBMP.Height - 1
                tmpBMP.Pixel2(pos + x, y) = Color.Silver
            Next
        Next
        For y As Integer = 0 To tmpBMP.Height - 1 : tmpBMP.Pixel2(pos, y) = Color.Silver : Next
        tmpBMP.UnLockBitmap()

        ' Z
        tmpBMP = New TheiaLib.Imaging.GDIBitmap(pbZ.Image)
        For x As Integer = 0 To tmpBMP.Width - 1
            RGB = TheiaLib.Imaging.ColorSpaceConverter.XYZ2RGB(New TheiaLib.Imaging.XYZTriplet(XYZ.X, XYZ.Y, x / tmpBMP.Width))
            p = Color.FromArgb(Math.Min(Math.Max(Math.Round(RGB.Red * 255), 0), 255), _
                               Math.Min(Math.Max(Math.Round(RGB.Green * 255), 0), 255), _
                               Math.Min(Math.Max(Math.Round(RGB.Blue * 255), 0), 255))
            For y As Integer = 0 To tmpBMP.Height - 1
                tmpBMP.Pixel(x, y) = p
            Next
        Next
        pos = XYZ.Z * (tmpBMP.Width - 1)
        For x As Integer = (tmpBMP.Height / 3) * -1 To (tmpBMP.Height / 3)
            For y As Integer = tmpBMP.Height / 3 * 2 + Math.Abs(x) To tmpBMP.Height - 1
                tmpBMP.Pixel2(pos + x, y) = Color.Silver
            Next
        Next
        For y As Integer = 0 To tmpBMP.Height - 1 : tmpBMP.Pixel2(pos, y) = Color.Silver : Next
        tmpBMP.UnLockBitmap()



        PictureBox2.Refresh()

        pbRed.Refresh()
        pbGreen.Refresh()
        pbBlue.Refresh()

        pbHue.Refresh()
        pbSaturation.Refresh()
        pbValue.Refresh()

        pbLuminance.Refresh()
        pbA.Refresh()
        pbB.Refresh()

        pbX.Refresh()
        pbY.Refresh()
        pbZ.Refresh()


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PictureBox2.Image = New Drawing.Bitmap(PictureBox2.Width, PictureBox2.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)

        pbRed.Image = New Drawing.Bitmap(pbRed.Width, pbRed.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbGreen.Image = New Drawing.Bitmap(pbGreen.Width, pbGreen.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbBlue.Image = New Drawing.Bitmap(pbBlue.Width, pbBlue.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)

        pbHue.Image = New Drawing.Bitmap(pbHue.Width, pbHue.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbSaturation.Image = New Drawing.Bitmap(pbSaturation.Width, pbSaturation.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbValue.Image = New Drawing.Bitmap(pbValue.Width, pbValue.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)

        pbX.Image = New Drawing.Bitmap(pbX.Width, pbX.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbY.Image = New Drawing.Bitmap(pbY.Width, pbY.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbZ.Image = New Drawing.Bitmap(pbZ.Width, pbZ.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)

        pbLuminance.Image = New Drawing.Bitmap(pbLuminance.Width, pbLuminance.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbA.Image = New Drawing.Bitmap(pbA.Width, pbA.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbB.Image = New Drawing.Bitmap(pbB.Width, pbB.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)

        UpdateColorSlider()

    End Sub

    Private Sub nudRGB_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudGreen.ValueChanged, nudBlue.ValueChanged, nudRed.ValueChanged
        If Not _SuspendUpdate Then
            _SuspendUpdate = True
            Dim RGB As New TheiaLib.Imaging.RGBTriplet(nudRed.Value, nudGreen.Value, nudBlue.Value)
            Dim HSV As TheiaLib.Imaging.HSVTriplet = TheiaLib.Imaging.ColorSpaceConverter.RGB2HSV(RGB)
            Dim XYZ As TheiaLib.Imaging.XYZTriplet = TheiaLib.Imaging.ColorSpaceConverter.RGB2XYZ(RGB)
            Dim Lab As TheiaLib.Imaging.LabTriplet = TheiaLib.Imaging.ColorSpaceConverter.XYZ2Lab(XYZ)

            nudHue.Value = Math.Min(Math.Max(HSV.Hue, nudHue.Minimum), nudHue.Maximum)
            nudSaturation.Value = Math.Min(Math.Max(HSV.Saturation, nudSaturation.Minimum), nudSaturation.Maximum)
            nudValue.Value = Math.Min(Math.Max(HSV.Value, nudValue.Minimum), nudValue.Maximum)

            nudX.Value = Math.Max(Math.Min(XYZ.X, nudX.Maximum), nudX.Minimum)
            nudY.Value = Math.Max(Math.Min(XYZ.Y, nudY.Maximum), nudY.Minimum)
            nudZ.Value = Math.Max(Math.Min(XYZ.Z, nudZ.Maximum), nudZ.Minimum)

            nudLuminance.Value = Math.Min(Math.Max(Lab.Luminance, nudLuminance.Minimum), nudLuminance.Maximum)
            nudA.Value = Math.Min(Math.Max(Lab.a, nudA.Minimum), nudA.Maximum)
            nudB.Value = Math.Min(Math.Max(Lab.b, nudB.Minimum), nudB.Maximum)

            UpdateColorSlider()
            _SuspendUpdate = False
        End If
    End Sub

    Private Sub nudHSV_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudHue.ValueChanged, nudValue.ValueChanged, nudSaturation.ValueChanged
        If Not _SuspendUpdate Then
            _SuspendUpdate = True
            Dim HSV As New TheiaLib.Imaging.HSVTriplet(nudHue.Value, nudSaturation.Value, nudValue.Value)
            Dim RGB As TheiaLib.Imaging.RGBTriplet = TheiaLib.Imaging.ColorSpaceConverter.HSV2RGB(HSV)
            Dim XYZ As TheiaLib.Imaging.XYZTriplet = TheiaLib.Imaging.ColorSpaceConverter.RGB2XYZ(RGB)
            Dim Lab As TheiaLib.Imaging.LabTriplet = TheiaLib.Imaging.ColorSpaceConverter.XYZ2Lab(XYZ)

            nudRed.Value = Math.Max(Math.Min(RGB.Red, nudRed.Maximum), nudRed.Minimum)
            nudGreen.Value = Math.Max(Math.Min(RGB.Green, nudGreen.Maximum), nudGreen.Minimum)
            nudBlue.Value = Math.Max(Math.Min(RGB.Blue, nudBlue.Maximum), nudBlue.Minimum)

            nudX.Value = Math.Max(Math.Min(XYZ.X, nudX.Maximum), nudX.Minimum)
            nudY.Value = Math.Max(Math.Min(XYZ.Y, nudY.Maximum), nudY.Minimum)
            nudZ.Value = Math.Max(Math.Min(XYZ.Z, nudZ.Maximum), nudZ.Minimum)

            nudLuminance.Value = Math.Min(Math.Max(Lab.Luminance, nudLuminance.Minimum), nudLuminance.Maximum)
            nudA.Value = Math.Min(Math.Max(Lab.a, nudA.Minimum), nudA.Maximum)
            nudB.Value = Math.Min(Math.Max(Lab.b, nudB.Minimum), nudB.Maximum)

            UpdateColorSlider()
            _SuspendUpdate = False
        End If
    End Sub

    Private Sub nudLab_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudLuminance.ValueChanged, nudB.ValueChanged, nudA.ValueChanged
        If Not _SuspendUpdate Then
            _SuspendUpdate = True
            Dim Lab As New TheiaLib.Imaging.LabTriplet(nudLuminance.Value, nudA.Value, nudB.Value)
            Dim XYZ As TheiaLib.Imaging.XYZTriplet = TheiaLib.Imaging.ColorSpaceConverter.Lab2XYZ(Lab)
            Dim RGB As TheiaLib.Imaging.RGBTriplet = TheiaLib.Imaging.ColorSpaceConverter.XYZ2RGB(XYZ)
            Dim HSV As TheiaLib.Imaging.HSVTriplet = TheiaLib.Imaging.ColorSpaceConverter.RGB2HSV(RGB)

            nudRed.Value = Math.Max(Math.Min(RGB.Red, nudRed.Maximum), nudRed.Minimum)
            nudGreen.Value = Math.Max(Math.Min(RGB.Green, nudGreen.Maximum), nudGreen.Minimum)
            nudBlue.Value = Math.Max(Math.Min(RGB.Blue, nudBlue.Maximum), nudBlue.Minimum)

            nudHue.Value = Math.Min(Math.Max(HSV.Hue, nudHue.Minimum), nudHue.Maximum)
            nudSaturation.Value = Math.Min(Math.Max(HSV.Saturation, nudSaturation.Minimum), nudSaturation.Maximum)
            nudValue.Value = Math.Min(Math.Max(HSV.Value, nudValue.Minimum), nudValue.Maximum)

            nudX.Value = Math.Max(Math.Min(XYZ.X, nudX.Maximum), nudX.Minimum)
            nudY.Value = Math.Max(Math.Min(XYZ.Y, nudY.Maximum), nudY.Minimum)
            nudZ.Value = Math.Max(Math.Min(XYZ.Z, nudZ.Maximum), nudZ.Minimum)

            UpdateColorSlider()
            _SuspendUpdate = False
        End If
    End Sub

    Private Sub nudXYZ_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudX.ValueChanged, nudZ.ValueChanged, nudY.ValueChanged
        If Not _SuspendUpdate Then
            _SuspendUpdate = True
            Dim XYZ As New TheiaLib.Imaging.XYZTriplet(nudX.Value, nudY.Value, nudZ.Value)
            Dim Lab As TheiaLib.Imaging.LabTriplet = TheiaLib.Imaging.ColorSpaceConverter.XYZ2Lab(XYZ)
            Dim RGB As TheiaLib.Imaging.RGBTriplet = TheiaLib.Imaging.ColorSpaceConverter.XYZ2RGB(XYZ)
            Dim HSV As TheiaLib.Imaging.HSVTriplet = TheiaLib.Imaging.ColorSpaceConverter.RGB2HSV(RGB)

            nudRed.Value = Math.Max(Math.Min(RGB.Red, nudRed.Maximum), nudRed.Minimum)
            nudGreen.Value = Math.Max(Math.Min(RGB.Green, nudGreen.Maximum), nudGreen.Minimum)
            nudBlue.Value = Math.Max(Math.Min(RGB.Blue, nudBlue.Maximum), nudBlue.Minimum)

            nudHue.Value = Math.Min(Math.Max(HSV.Hue, nudHue.Minimum), nudHue.Maximum)
            nudSaturation.Value = Math.Min(Math.Max(HSV.Saturation, nudSaturation.Minimum), nudSaturation.Maximum)
            nudValue.Value = Math.Min(Math.Max(HSV.Value, nudValue.Minimum), nudValue.Maximum)

            nudLuminance.Value = Math.Min(Math.Max(Lab.Luminance, nudLuminance.Minimum), nudLuminance.Maximum)
            nudA.Value = Math.Min(Math.Max(Lab.a, nudA.Minimum), nudA.Maximum)
            nudB.Value = Math.Min(Math.Max(Lab.b, nudB.Minimum), nudB.Maximum)

            UpdateColorSlider()
            _SuspendUpdate = False
        End If
    End Sub

    Private Sub pbRed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbRed.Click
        nudRed.Value = pbRed.PointToClient(MousePosition).X / pbRed.Width
    End Sub

    Private Sub pbGreen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbGreen.Click
        nudGreen.Value = pbGreen.PointToClient(MousePosition).X / pbGreen.Width
    End Sub

    Private Sub pbBlue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbBlue.Click
        nudBlue.Value = pbBlue.PointToClient(MousePosition).X / pbBlue.Width
    End Sub

    Private Sub pbHue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbHue.Click
        nudHue.Value = pbHue.PointToClient(MousePosition).X / pbHue.Width
    End Sub

    Private Sub pbSaturation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbSaturation.Click
        nudSaturation.Value = pbSaturation.PointToClient(MousePosition).X / pbSaturation.Width
    End Sub

    Private Sub pbValue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbValue.Click
        nudValue.Value = pbValue.PointToClient(MousePosition).X / pbValue.Width
    End Sub

    Private Sub pbLuminance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbLuminance.Click
        nudLuminance.Value = pbLuminance.PointToClient(MousePosition).X / pbLuminance.Width
    End Sub

    Private Sub pbA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbA.Click
        nudA.Value = -1.2 + pbA.PointToClient(MousePosition).X / pbA.Width * 2.4
    End Sub

    Private Sub pbB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbB.Click
        nudB.Value = -1.2 + pbB.PointToClient(MousePosition).X / pbB.Width * 2.4
    End Sub

    Private Sub pbX_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbX.Click
        nudX.Value = pbX.PointToClient(MousePosition).X / pbX.Width
    End Sub

    Private Sub pbY_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbY.Click
        nudY.Value = pbY.PointToClient(MousePosition).X / pbY.Width
    End Sub

    Private Sub pbZ_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbZ.Click
        nudZ.Value = pbZ.PointToClient(MousePosition).X / pbZ.Width
    End Sub

    Dim tmpBmp As TheiaLib.Imaging.HDRfiles.RadianceHDR_Image

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        _tmpHDRBmp = New TheiaLib.Imaging.HDRfiles.RadianceHDR_Image(TextBox1.Text)
        pbHDR_Bitmap.Width = _tmpHDRBmp.Image.Width
        pbHDR_Bitmap.Height = _tmpHDRBmp.Image.Height
        pbHDR_Bitmap.Image = _tmpHDRBmp.Image.AsBitmap

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        _tmpHDRBmp.Save(TextBox1.Text & "save.hdr")

    End Sub

    Private Sub HandleHDRBMP_Progress(ByVal info As TheiaLib.Imaging.HDRfiles.RadianceHDR_Image_ProgressInfo) Handles _tmpHDRBmp.Progress
        Select Case info.ProgressType
            Case TheiaLib.Imaging.HDRfiles.RadianceHDR_Image_ProgressType.Started
                ProgressBar1.Visible = True
                ProgressBar1.Value = 0
            Case TheiaLib.Imaging.HDRfiles.RadianceHDR_Image_ProgressType.Finished
                ProgressBar1.Visible = False
            Case Else
                ProgressBar1.Value = info.Percentage * 100
        End Select
        ProgressBar1.Refresh()
    End Sub

    Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        PictureBox2.Image = New Drawing.Bitmap(PictureBox2.Width, PictureBox2.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)

        pbRed.Image = New Drawing.Bitmap(pbRed.Width, pbRed.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbGreen.Image = New Drawing.Bitmap(pbGreen.Width, pbGreen.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbBlue.Image = New Drawing.Bitmap(pbBlue.Width, pbBlue.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)

        pbHue.Image = New Drawing.Bitmap(pbHue.Width, pbHue.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbSaturation.Image = New Drawing.Bitmap(pbSaturation.Width, pbSaturation.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbValue.Image = New Drawing.Bitmap(pbValue.Width, pbValue.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)

        pbX.Image = New Drawing.Bitmap(pbX.Width, pbX.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbY.Image = New Drawing.Bitmap(pbY.Width, pbY.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbZ.Image = New Drawing.Bitmap(pbZ.Width, pbZ.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)

        pbLuminance.Image = New Drawing.Bitmap(pbLuminance.Width, pbLuminance.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbA.Image = New Drawing.Bitmap(pbA.Width, pbA.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)
        pbB.Image = New Drawing.Bitmap(pbB.Width, pbB.Height, Drawing.Imaging.PixelFormat.Format32bppRgb)

        UpdateColorSlider()
    End Sub
End Class
